package com.daikin.ktmustwrite.ui.home

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.daikin.ktmustwrite.R
import com.daikin.ktmustwrite.app.base.BaseFragment
import com.daikin.ktmustwrite.app.ext.*
import com.daikin.ktmustwrite.app.weight.banner.HomeBannerAdapter
import com.daikin.ktmustwrite.app.weight.banner.HomeBannerViewHolder
import com.daikin.ktmustwrite.app.weight.recycleview.DefineLoadMoreView
import com.daikin.ktmustwrite.app.weight.recycleview.SpaceItemDecoration
import com.daikin.ktmustwrite.data.model.bean.BannerResponse
import com.daikin.ktmustwrite.databinding.FragmentHomeBinding
import com.daikin.ktmustwrite.ui.adapter.AriticleAdapter
import com.daikin.ktmustwrite.viewmodel.request.RequestHomeViewModel
import com.daikin.ktmustwrite.viewmodel.state.HomeViewModel
import com.google.gson.Gson
import com.kingja.loadsir.core.LoadService
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import com.zhpan.bannerview.BannerViewPager
import kotlinx.android.synthetic.main.include_recyclerview.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.parseState

class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>() {

    private val articleAdapter :AriticleAdapter by lazy {  AriticleAdapter(arrayListOf(),true)}
    private val requestHomeViewModel: RequestHomeViewModel by viewModels()
    //recyclerview的底部加载view 因为在首页要动态改变他的颜色，所以加了他这个字段

    private lateinit var loadSir: LoadService<Any>
    //recyclerview的底部加载view 因为在首页要动态改变他的颜色，所以加了他这个字段
    private lateinit var footView: DefineLoadMoreView

    override fun layoutId(): Int {
        return R.layout.fragment_home
    }

    override fun initView(savedInstanceState: Bundle?) {
        loadSir = loadServiceInit(swipeRefresh) {
            loadSir.showLoading()
            requestHomeViewModel.getBannerData()
            requestHomeViewModel.getHomeData(true)
        }

        //初始化recyclerView
        recyclerView.init(LinearLayoutManager(context), articleAdapter).let {
            //因为首页要添加轮播图，所以我设置了firstNeedTop字段为false,即第一条数据不需要设置间距
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f), false))
            footView = it.initFooter(SwipeRecyclerView.LoadMoreListener {
                requestHomeViewModel.getHomeData(false)
            })
            //初始化FloatingActionButton
//            it.initFloatBtn(floatbtn)
        }

        //初始化 SwipeRefreshLayout
        swipeRefresh.init {
            //触发刷新监听时请求数据
            requestHomeViewModel.getHomeData(true)
        }
    }

    override fun lazyLoadData() {
        //请求轮播图数据
        requestHomeViewModel.getBannerData()
    }

    override fun createObserver() {
        requestHomeViewModel.run {

            //监听首页文章列表请求的数据变化
            homeDataState.observe(viewLifecycleOwner, Observer {
                //设值 新写了个拓展函数，搞死了这个恶心的重复代码
                loadListData(it, articleAdapter, loadSir, recyclerView, swipeRefresh)
            })

            bannerData.observe(viewLifecycleOwner, Observer { resultState ->
                parseState(resultState, { data ->
                    if (recyclerView.headerCount == 0) {
                        val headview =
                            LayoutInflater.from(context).inflate(R.layout.include_banner, null)
                                .apply {
                                    findViewById<BannerViewPager<BannerResponse, HomeBannerViewHolder>>(
                                        R.id.banner_view
                                    )
                                        .apply {
                                            adapter = HomeBannerAdapter()
                                            setLifecycleRegistry(lifecycle)
                                            setOnPageClickListener {
                                            }
                                            create(data)
                                        }
                                }
                        recyclerView.addHeaderView(headview)
                    }
                })
            })
        }
    }
}
